require("lua-xwrt.addon.string")
require("lua-xwrt.addon.uci")
require("lua-xwrt.html.form")
require("lua-xwrt.xwrt.translator")
require("lua-xwrt.addon.io")
require("lua-xwrt.html.messages")
require("lua-xwrt.html.tbform")

local newMenu = htmlhmenuClass.new("submenu")
newMenu:add(tr("freeradius_menu_serverive#Service"),"freeradius2.sh")
newMenu:add(tr("freeradius_menu_users#Users"),"freeradius2.sh?option=users")

if __MENU[__FORM.cat].len > 1 then
	__MENU[__FORM.cat]["Freeradius"] = newMenu
else
	__MENU[__FORM.cat]= newMenu
end

local string = string
local io = io
local os = os
local pairs, ipairs = pairs, ipairs
local table = table
local uci = uci
local util = util
local string = string
local type = type
local print = print
local tostring, tonumber = tostring, tonumber
local formClass = formClass
local htmlhmenuClass = htmlhmenuClass
local tbformClass = tbformClass
local tr = tr
local __ENV = __ENV
local __FORM = __FORM
module("lua-xwrt.freeradius2")

function default()
	local forms = {}
	forms[#forms+1] = formClass.new("Service")
	forms[#forms]:Add("service","freeradius.service.enable","radiusd",tr("radius_var_service#Radius"),"")
	forms[#forms]["freeradius.service.enable"].options:Add("service","radiusd")
	forms[#forms]["freeradius.service.enable"].options:Add("init","radiusd")
	return forms
end

function users ()
	local forms = {}
	forms[#forms+1] = add_usr_form()
	local freeradius_check = uci.get_all("freeradius_check")
	local freeradius_reply = uci.get_all("freeradius_reply")
	local users = uci.get_type("freeradius_check","user")
	local form = tbformClass.new("Local Users")
	
	form:Add_col("label", "Username","Username", "120px")
	form:Add_col("text", "Password", "Password", "120px","string,len>5","width:120px")
	form:Add_col("text", "Expiration", "Expiration", "120px","string","width:120px")
	form:Add_col("select", "FallThrough", "Fall Through", "100px","string","width:100px")
	form.col[form.col.FallThrough].options:Add("yes","Yes")
	form.col[form.col.FallThrough].options:Add("no","No")
	form:Add_col("text", "Simultaneous", "Simultaneous", "80px","int","width:80px")
	form:Add_col("text", "IdleTimeout", "Idle Timeout", "80px","int","width:80px")
	form:Add_col("text", "AcctInterimInt", "Acct Interim Interval", "90px","int","width:90px")
	form:Add_col("text", "MaxDown", "MaxDown", "100px","int","width:100px")
	form:Add_col("text", "MaxUp", "MaxUp", "100px","int","width:100px")
	form:Add_col("link", "Remove","Remove ", "100px","","width:100px")

	if users then
		for i=1, #users do
			local name = users[i][".name"]
			if name ~= "default" then
				local checks = freeradius_check[name]
				local replys = freeradius_reply[name]
				password = checks.User_Password or ""
				expiration = checks.Expiration or ""
				fall = replys.Fall_Through or ""
				simul = checks.Simultaneous_Use or ""
				itimeout = replys.Idle_Timeout or ""
				acctii = replys.Acct_Interim_Interval or ""
				maxdown = replys.WISPr_Bandwidth_Max_Down or ""
				maxup = replys.WISPr_Bandwidth_Max_Up or ""
				form:New_row()
				form:set_col("Username","freeradius_check."..name..".Username", name)
				form:set_col("Password","freeradius_check."..name..".User_Password", password)
				form:set_col("Expiration","freeradius_check."..name..".Expiration", expiration)
				form:set_col("FallThrough", "freeradius_reply."..name..".Fall_Through", fall)
				form:set_col("Simultaneous", "freeradius_check."..name..".Simultaneous_Use", simul)
				form:set_col("IdleTimeout", "freeradius_reply."..name..".Idle_Timeout", itimeout)
				form:set_col("AcctInterimInt", "freeradius_reply."..name..".Acct_Interim_Interval", acctii)
				form:set_col("MaxDown", "freeradius_reply."..name..".WISPr_Bandwidth_Max_Down", maxdown)
				form:set_col("MaxUp", "freeradius_reply."..name..".WISPr_Bandwidth_Max_Up", maxup)
				form:set_col("Remove", "Remove_"..name, __ENV.SCRIPT_NAME.."?".."UCI_CMD_delfreeradius_check."..name.."=&UCI_CMD_delfreeradius_reply."..name.."=&cat="..__FORM.cat.."&option="..__FORM.option)
			end
		end
	end
	forms[#forms+1] = form
	return forms
end

function add_usr_form(form,user_level)
	if __FORM["freeradius_check,freeradius_reply"] 
--	&& __FORM.UCI_SET_VALUE ~= "" 
	then
		uci.set("freeradius_check",__FORM.UCI_SET_VALUE,"user")
		uci.save("freeradius_check")
		uci.set("freeradius_reply",__FORM.UCI_SET_VALUE,"user")
		uci.save("freeradius_reply")
	end
	if form == nil then
    form = formClass.new("Local Users")
  else
    form:Add("subtitle","Local Users")
  end
  
  form:Add("uci_set_config","freeradius_check,freeradius_reply","user",tr("freerad_add_user#New User"),"string")
  return form
end
